using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using System.Threading;

namespace FEZ_Panda_II_Application1
{
    public class PowerControl
    {
        OutputPort p = new OutputPort(AstoXPinMap.PowerPin, true);
        public PowerControl()
        {   
            FlightControl.FlightPhaseChanged += new FlightPhaseChangeHandler(FlightControl_FlightPhaseChanged);
        }

        void FlightControl_FlightPhaseChanged(FlightPhases newphase)
        {
            if (newphase == FlightPhases.Ascent)
            {
                Program.Print("Switching power on");
                p.Write(false);//turn on back up power
                Timer t=new Timer(TimeOut,null,1000*60*30,0);
            }
        }

        void TimeOut(object state)
        {
            Program.Print("Shutting down");
            p.Write(true);//shut down power
            Program.Print("Shut down");//we should never get to this code!!
            FlightControl.CurrentPhase = FlightPhases.Shutdown; //we should never get to this state!
        }
    }
}
